#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

bool judge(int x)
{
    vector<int> count(10);
    while (x)
    {
        count[x % 10]++;
        x /= 10;
    }

    for (int i = 0; i < 10; i++)
    {
        if (count[i] > 0 && count[i] != i)
            return false;
    }
    return true;
}

int nextBeautifulNumber(int n) {
    for (int i = n + 1; i <= 1224444; i++)
        if (judge(i))
            return i;
    return -1;
}